/**
 * 
 */
package coop.tecso.core.test.handlers;

import java.util.Map;
import java.util.Set;

import org.org.usurper.handlers.IHandler;
import org.org.usurper.handlers.basic.AbstractPropertyTypeHandler;
import org.org.usurper.model.HandledBeanProperty;
import org.org.usurper.model.HandledConstructorArg;
import org.org.usurper.model.ITargetDefinition;
import org.org.usurper.model.PropertyTypeDefinition;
import org.org.usurper.setup.UsurperGeneratorSetup;

import coop.tecso.core.test.generator.UsurperGenerator;

/**
 * @author daniel
 *
 */
public class ValueObjectHandler extends AbstractPropertyTypeHandler {

    /**
     * @param propertyTypeDefinitionClasses
     */
    public ValueObjectHandler(Class<?>... propertyTypeDefinitionClasses) {
        super(propertyTypeDefinitionClasses);
        // TODO Auto-generated constructor stub
    }

    /**
     * @param properTypeDefinitions
     */
    public ValueObjectHandler(PropertyTypeDefinition... properTypeDefinitions) {
        super(properTypeDefinitions);
    }

    /**
     * @param handledTypes
     */
    public ValueObjectHandler(Set<PropertyTypeDefinition> handledTypes) {
        super(handledTypes);
    }

    /* (non-Javadoc)
     * @see org.org.usurper.handlers.IHandler#handle(org.org.usurper.model.HandledBeanProperty)
     */
    @Override
    public Object handle(HandledBeanProperty handledBeanProperty) {
        return doHandle(handledBeanProperty.getPropertyClass(), handledBeanProperty.getUsurperGeneratorSetup().getAllHandlers());
    }

    /* (non-Javadoc)
     * @see org.org.usurper.handlers.IHandler#handle(org.org.usurper.model.HandledConstructorArg)
     */
    @Override
    public Object handle(HandledConstructorArg handledConstructorArg) {
        return doHandle(handledConstructorArg.getConstructorArgClass(), handledConstructorArg.getUsurperGeneratorSetup().getAllHandlers());
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    private Object doHandle(Class<?> entityClass, Map<ITargetDefinition, IHandler> parentHandlers) {
        UsurperGeneratorSetup usurperGeneratorSetup = new UsurperGeneratorSetup();
        usurperGeneratorSetup.setAllHandlers(parentHandlers);
        UsurperGenerator<?> usurper = new UsurperGenerator(entityClass, usurperGeneratorSetup);
        return usurper.generateUsurper();
    }

}
